package com.module.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.view.WheelView;
import com.module.dialog.inter.DialogOnItemListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: dengqi
 * @CreateDate: 2020/4/16 13:59
 * @Description: 滚轮对话框
 */
public class PickerDialog<T> implements View.OnClickListener {

    private Context mContext;
    private TextView tvTitle; //标题
    private TextView btn1, btn2; //2个按钮
    private WheelView wheelView1;
    private View contentView;
    public String title;
    public List<T> items;
    public DialogOnItemListener onItemListener;
    public Dialog dialog;

    public PickerDialog(Context context) {
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_picker, null);
        tvTitle = contentView.findViewById(R.id.tv_title);
        btn1 = contentView.findViewById(R.id.button1);
        btn2 = contentView.findViewById(R.id.button2);
        wheelView1 = contentView.findViewById(R.id.wheelView1);
        items = new ArrayList<>();

        //是否循环
//        wheelView1.setCyclic(false);
        //文字大小
        wheelView1.setTextSize(14f);
        //item高度
        wheelView1.setLineSpacingMultiplier(2.5f);
        //显示item个数 默认11个
        wheelView1.setItemsVisibleCount(5);
    }

    public PickerDialog setItems(List<T> items, DialogOnItemListener listener) {
        this.items = items;
        this.onItemListener = listener;
        return this;
    }

    public PickerDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public Dialog build() {
        wheelView1.setAdapter(new ArrayWheelAdapter<>(items));
        btn1.setText("取消");
        btn1.setOnClickListener(this);
        btn2.setText("确定");
        btn2.setOnClickListener(this);

        //上段布局-标题
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }

        dialog = new Dialog(mContext, R.style.dialogStyle);
        dialog.setContentView(contentView);
        dialog.getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
        dialog.getWindow().getAttributes().gravity = Gravity.BOTTOM;
        dialog.getWindow().getAttributes().windowAnimations = R.style.picker_view_slide_anim;
        return dialog;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button1) {
            dialog.dismiss();
        } else if (v.getId() == R.id.button2) {
            if (onItemListener != null) {
                onItemListener.onItemClick(wheelView1.getCurrentItem());
            }
            dialog.dismiss();
        }
    }

}
